﻿Imports System.Runtime.Serialization

<Serializable()> _
Public Class Authority
    Implements ISerializable

#Region "Properties"

    Private _code As Integer
    Public Property UniqueCode() As Integer
        Get
            Return _code
        End Get
        Set(ByVal value As Integer)
            _code = value
        End Set
    End Property


    Private _name As String
    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property


    Private _valid As Boolean
    Public Property IsValid() As Boolean
        Get
            Return _valid
        End Get
        Set(ByVal value As Boolean)
            _valid = value
        End Set
    End Property


    Private _update As Date
    Public Property DateChanged() As Date
        Get
            Return _update
        End Get
        Set(ByVal value As Date)
            _update = value
        End Set
    End Property


#End Region

#Region "Creation"

    Protected Sub New()
    End Sub

    Public Sub New(ByVal info As SerializationInfo, _
                   ByVal context As StreamingContext)
        Me.UniqueCode = info.GetInt32("UniqueCode")
        Me.Name = info.GetString("Name")
        Me.DateChanged = info.GetDateTime("DateChanged")
        Me.IsValid = info.GetBoolean("IsValid")
    End Sub

    Public Sub GetObjectData(ByVal info As SerializationInfo, _
                             ByVal context As StreamingContext) _
                             Implements ISerializable.GetObjectData
        info.AddValue("UniqueCode", Me.UniqueCode)
        info.AddValue("Name", Me.Name)
        info.AddValue("DateChanged", Me.DateChanged)
        info.AddValue("IsValid", Me.IsValid)
    End Sub

    Public Shared Function CreateNewAuthority() As Authority
        Return New Authority()
    End Function

    Public Shared Function CreateAuthority(ByVal code As Integer, _
                                      ByVal name As String, _
                                      ByVal update As Date, _
                                      ByVal valid As Boolean) As Authority
        Dim authority As Authority = New Authority()
        With authority
            authority.UniqueCode = code
            authority.Name = name
            authority.DateChanged = update
            authority.IsValid = valid
        End With

        Return authority
    End Function

#End Region

#Region "Overriden Object"

    Public Overrides Function GetHashCode() As Integer
        Dim hashCode As Integer = 0
        hashCode += 1000000001 * Me.UniqueCode.GetHashCode()
        hashCode += 1000000002 * Me.Name.GetHashCode()
        hashCode += 1000000003 * Me.DateChanged.GetHashCode()
        hashCode += 1000000004 * Me.IsValid.GetHashCode()
        Return hashCode
    End Function

    Public Overrides Function Equals(ByVal obj As Object) As Boolean
        Dim other As Authority = TryCast(obj, Authority)
        If other Is Nothing Then
            Return False
        End If

        Return Me.UniqueCode = other.UniqueCode
    End Function

#End Region

End Class
